﻿using System.Data.Entity;

using Cinema.Model.Entities;

namespace Cinema.Model.EF
{
    public class CinemaContext : DbContext
    {
        public CinemaContext()
            : base("name=CinemaConnection")
        {
        }

        public DbSet<Actor> Actors { get; set; }
        public DbSet<Genre> Genres { get; set; }
        public DbSet<Movie> Movies { get; set; }
        public DbSet<MovieType> MovieTypes { get; set; }
        public DbSet<Projection> Projections { get; set; }
        public DbSet<Reservation> Reservations { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Movie>().HasMany(x => x.Genres).WithMany();
            modelBuilder.Entity<Movie>().HasMany(x => x.Types).WithMany();
            modelBuilder.Entity<Movie>().HasMany(x => x.Actors).WithMany(x => x.Movies);
            modelBuilder.Entity<Movie>().HasMany(x => x.Projections).WithRequired(x => x.Movie);
            modelBuilder.Entity<Projection>().HasMany(x => x.Reservations).WithRequired();
            modelBuilder.Entity<Projection>().HasRequired(x => x.Room).WithMany(x => x.Projections);
            modelBuilder.Entity<Reservation>().HasRequired(x => x.User).WithMany();

            modelBuilder.Entity<Genre>().Property(x => x.Name)
                .IsRequired().HasMaxLength(25);
        }
    }
}
